localStorage.clear()
$(function () {
    let name = localStorage.getItem("name");
    let password = localStorage.getItem("password");
    if (name && password) {
        $("#username").val(`${name}`);
        $("userpass").val(`${password}`);
        $("#btn").click()
    }
})

// 读cookie
function getCookie(key) {
    let cookie = document.cookie;
    let arr = cookie.split("; ");
    let result = {}
    arr.forEach(item => {
        let key = item.split("=")[0];
        let value = item.split("=")[1];
        result[key] = value;
    })

    if (key) {
        return result[key];
    }
    return result;
}
// 删cookie
function removeCookie(key) {
    let guoqu = new Date("1970-01-01 00:00:00")
    if (key) {
        document.cookie = `${key}=beybey;expires=${guoqu}`
    } else {
        let cookie = getCookie();

        for (let i in cookie) {
            document.cookie = `${i}=beybey;expires=${guoqu}`
        }
    }
}
// 写cookie
function setCookie(key, value, expires) {
    if (typeof expires === "number") {
        let date = new Date();
        date.setDate(date.getDate() + expires)
        document.cookie = `${key}=${value};expires=${date}`;
    } else {
        document.cookie = `${key}=${value};expires=${expires}`;
    }
}

$("#btn").click(function () {
    let user = $("#username");
    let password = $("#userpass");
    $.ajax({
        url: "http://10.35.161.229:81/login",
        data: {
            name: user.val(),
            password: password.val()
        },
        method: "GET",
        success: function (data) {
            if (data === "null") {
                alert("用户不存在")
                return;
            };
            if (data === "passwordError") {
                alert("用户密码错误");
                return;
            };
            if (data === "success") {
                console.log($("#check").prop("checked"))
                if ($("#check").prop("checked") == true) {
                    setCookie("name", user.val(), 1)
                }
                location.href = "index.html";
            }
        }
    })

})